package com.hncc.dms.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hncc.dms.domain.DmsDise;
import com.hncc.dms.service.IDmsDiseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.hncc.dms.domain.DmsCaseHistory;
import com.hncc.dms.service.IDmsCaseHistoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;

/**
 * 病历Controller
 *
 * @author ruoyi
 * @date 2020-09-12
 */
@RestController
@RequestMapping("/dms/case_history")
@Api(tags = "病历接口")
public class DmsCaseHistoryController extends BaseController
{
    @Resource
    private IDmsCaseHistoryService dmsCaseHistoryService;

    @Resource
    private IDmsDiseService dmsDiseService;

    /**
     * 查询病历列表
     */
    @ApiOperation(value = "条件分页查询病历",httpMethod = "GET")
    @PreAuthorize("@ss.hasPermi('dms:case_history:list')")
    @GetMapping("/list")
    public TableDataInfo list(DmsCaseHistory dmsCaseHistory)
    {
        startPage();
        List<DmsCaseHistory> list = dmsCaseHistoryService.selectDmsCaseHistoryList(dmsCaseHistory);
        return getDataTable(list);
    }

    /**
     * 导出病历列表
     */
    @PreAuthorize("@ss.hasPermi('dms:case_history:export')")
    @Log(title = "病历", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DmsCaseHistory dmsCaseHistory)
    {
        List<DmsCaseHistory> list = dmsCaseHistoryService.selectDmsCaseHistoryList(dmsCaseHistory);
        ExcelUtil<DmsCaseHistory> util = new ExcelUtil<DmsCaseHistory>(DmsCaseHistory.class);
        return util.exportExcel(list, "case_history");
    }

    /**
     * 获取病历详细信息
     */
    @ApiOperation(value = "根据id获取病历详细信息",httpMethod = "GET",
            notes = "根据id查询病历")
    @ApiImplicitParam(paramType = "path",name = "id",value = "病历id",dataType = "Long",required = true)
    @PreAuthorize("@ss.hasPermi('dms:case_history:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(dmsCaseHistoryService.selectDmsCaseHistoryById(id));
    }

    /**
     * 新增病历
     */
    @ApiOperation(value = "新增",httpMethod = "POST", notes = "新增病历")
    @ApiImplicitParam(name = "dmsCaseHistory", value = "新增病历", dataType = "DmsCaseHistory")
    @PreAuthorize("@ss.hasPermi('dms:case_history:add')")
    @Log(title = "病历", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DmsCaseHistory dmsCaseHistory)
    {
        return toAjax(dmsCaseHistoryService.insertDmsCaseHistory(dmsCaseHistory));
    }

    /**
     * 修改病历
     */
    @ApiOperation(value = "修改", httpMethod = "PUT", notes = "修改病历")
    @ApiImplicitParam(name = "dmsCaseHistory", value = "修改病历", dataType = "DmsCaseHistory")
    @PreAuthorize("@ss.hasPermi('dms:case_history:edit')")
    @Log(title = "病历", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DmsCaseHistory dmsCaseHistory)
    {
        return toAjax(dmsCaseHistoryService.updateDmsCaseHistory(dmsCaseHistory));
    }

    /**
     * 删除病历
     */
    @ApiOperation(value = "批量删除", httpMethod = "DELETE", notes = "根据ids删除病历")
    @ApiImplicitParam(paramType = "path",name = "ids",value = "病历id",dataType = "Long[]",required = true)
    @PreAuthorize("@ss.hasPermi('dms:case_history:remove')")
    @Log(title = "病历", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dmsCaseHistoryService.deleteDmsCaseHistoryByIds(ids));
    }

    /**
     * 根据挂号id获取初诊病历
     * @param registrationId
     * @return
     */
    @GetMapping("/getByRegistrationId/{registrationId}")
    public AjaxResult getByRegistrationId(@PathVariable Integer registrationId){
        Map<String,Object> map=new HashMap<String, Object>();
        DmsCaseHistory dmsCaseHistory=dmsCaseHistoryService.selectCaseHistoryByRegistrationId(registrationId);
        if (dmsCaseHistory!=null){
            String ids=dmsCaseHistory.getPriliminaryDiseIdList();
            List<DmsDise> dmsDises=dmsDiseService.selectDmsDiseByIds(ids);
            map.put("caseHistory",dmsCaseHistory);
            map.put("dmsDises",dmsDises);
            return AjaxResult.success(map);
        }
        return null;
    }
}
